package com.luli.controllor;

import com.luli.entity.Signature;
import com.luli.service.PdfSignatureService;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 陆离
 * 2024/11/12
 */
@RestController
@Slf4j
@RequestMapping("/pdf")
@CrossOrigin
public class PdfController {
    @Autowired
    private PdfSignatureService pdfSignatureService;

    /**
     * https://blog.csdn.net/lhmyy521125/article/details/143189490
     *
     * @param pdfFile
     * @param signatureImage
     * @return
     * @throws IOException
     */
    @PostMapping("/sign-pdf")
    public ResponseEntity<byte[]> signPdf(
            @RequestParam("pdfFile") MultipartFile pdfFile,
            @RequestParam("signatureImage") MultipartFile signatureImage) throws IOException {

        // 临时保存上传的 PDF 文件和签章图片
        String inputPdfPath = "C:\\Users\\lenovo\\Desktop\\江天兰台\\电子签章测试\\shuchu\\temp_input.pdf";
        String signatureImagePath = "C:\\Users\\lenovo\\Desktop\\江天兰台\\电子签章测试\\shuchu\\temp_signature.png";
        String outputPdfPath = "C:\\Users\\lenovo\\Desktop\\江天兰台\\电子签章测试\\shuchu\\signed_output.pdf";

        pdfFile.transferTo(new File(inputPdfPath));
        signatureImage.transferTo(new File(signatureImagePath));

        // 执行签章操作
        pdfSignatureService.signPdf(inputPdfPath, outputPdfPath, signatureImagePath);

        // 读取签章后的 PDF 文件并返回
        File signedPdf = new File(outputPdfPath);
        byte[] pdfBytes = java.nio.file.Files.readAllBytes(signedPdf.toPath());

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PDF);
        headers.setContentDispositionFormData("attachment", "signed_output.pdf");

        return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);
    }

    /**
     * https://blog.csdn.net/bjzhang75/article/details/142454964?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-2-142454964-blog-52717441.235%5Ev43%5Epc_blog_bottom_relevance_base1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-2-142454964-blog-52717441.235%5Ev43%5Epc_blog_bottom_relevance_base1&utm_relevant_index=5
     *
     * @param inputPdf
     * @param image
     * @return
     * @throws IOException
     */
    @PostMapping("/add")
    public ResponseEntity<String> addSignature(@RequestParam("inputPdf") MultipartFile inputPdf,
                                               @RequestParam("image") MultipartFile image
    ) throws IOException {

        String tempInputPdfPath = "C:\\Users\\lenovo\\Desktop\\江天兰台\\电子签章测试\\temp_input.pdf";
        String tempOutputPdfPath = "temp_input.pdf";
        String tempImagePath = "C:\\Users\\lenovo\\Desktop\\江天兰台\\电子签章测试\\temp_image.png";

        inputPdf.transferTo(new File(tempInputPdfPath));
        image.transferTo(new File(tempImagePath));

        Signature signature = new Signature();
        signature.setX(100);
        signature.setY(100);
        signature.setWidth(100);
        signature.setHeight(100);
        signature.setImagePath(tempImagePath);

        pdfSignatureService.addSignatureToPdf(tempInputPdfPath, tempOutputPdfPath, signature);

        Files.copy(Paths.get(tempOutputPdfPath), Paths.get("C:\\Users\\lenovo\\Desktop\\江天兰台\\电子签章测试\\" + tempOutputPdfPath));

        return ResponseEntity.ok().body("Signature added successfully!");
    }


    /**
     * 添加水印
     */
    @PostMapping("/shuiyin")
    public void shuiyin() throws IOException {
        PDDocument document = PDDocument.load(new File("C:\\Users\\lenovo\\Desktop\\江天兰台\\电子签章测试\\日志提交.pdf"));
        PDPage page = document.getPage(0);
        PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
        contentStream.beginText();
        contentStream.setFont(PDType1Font.HELVETICA_BOLD, 60);
        contentStream.setLeading(60 * 1.2f);
        contentStream.newLineAtOffset(page.getMediaBox().getWidth() / 2, page.getMediaBox().getHeight() / 2);
        contentStream.showText("test");
        contentStream.endText();
        contentStream.close();
        document.save(new File("C:\\Users\\lenovo\\Desktop\\江天兰台\\电子签章测试\\日志提交-水印.pdf"));
        document.close();
    }

}
